#include "Base.h"
#include "DBPlayer.h"
#include "Log.h"
#include "Product.h"
#include "MatchServer.h"

CDBPlayer *g_pDBPlayer = NULL;

CDBPlayer::CDBPlayer(void)
{
	
}

CDBPlayer::~CDBPlayer(void)
{
	g_pDBPlayer = NULL;
}

BOOL CDBPlayer::OnInitialUpdate(const char *szServerIP, int nPort, int nOrigin, int nServerId)
{
	if(!CServerPlayer::OnInitialUpdate(szServerIP, nPort, nOrigin, nServerId))
	{
		WriteLog("%s : %d CDBPlayer::OnInitialUpdate return FALSE", __FILE__, __LINE__);
		return FALSE;
	}
	g_pDBPlayer = this;
	return TRUE;
}

Msg* CDBPlayer::CreateMsg(DWORD dwMsgId, int nLen)
{
	switch(dwMsgId)
	{
	case Msg_DM_Execute_SignCost:
		{
			return new DMExecuteSignCostMsg;
		}
	case Msg_DM_Load_Game_Attr:
		{
			return new DMLoadGameAttrMsg;
		}
	default:
		break;
	}
    return NULL;
}

BOOL CDBPlayer::OnMsg(Msg *pMsg)
{
	DWORD dwMsgId = pMsg->GetMsgId();
	switch(dwMsgId)
	{
	case Msg_DM_Execute_SignCost:
		{
            AckExecuteSignCost((DMExecuteSignCostMsg*)pMsg);
		}
		break;
	case Msg_DM_Load_Game_Attr:
		{
			AckLoadGameAttr((DMLoadGameAttrMsg*)pMsg);
		}
		break;
	default:
		break;
	}

    return TRUE;
}

BOOL CDBPlayer::ReqExecuteSignCost(LMSignupMsg *pMsg, CLobbyPlayer *pLobbyPlayer, CProduct *pProduct)
{
	MDExecuteSignCostMsg msg;
	msg.m_dwClientId = pMsg->m_dwClientId;
    msg.m_nLobbyServerId = pLobbyPlayer->GetServerId();

	msg.m_SignUser.dwUserId = pMsg->m_dwUserId;
	strncpy(msg.m_SignUser.szUserName, pMsg->m_szUserName, sizeof(msg.m_SignUser.szUserName) - 1);
	msg.m_SignUser.tmSignup = time(NULL);
	msg.m_SignUser.dwSignupIP = pMsg->m_dwSignupIP;
	msg.m_SignUser.m_nFigure = pMsg->m_nFigure;

	msg.m_SignCost = pProduct->m_stSignCost;

	SendMsg(&msg);

    return TRUE;
}

BOOL CDBPlayer::AckExecuteSignCost(DMExecuteSignCostMsg *pMsg)
{
	CProduct *pProduct = g_pMatchServer->FindProduct(pMsg->m_dwProductId);
	if(pProduct == NULL)
	{
		WriteLog("%s : %d, AckExecuteSignCost pProduct == NULL ProductId=%d", pMsg->m_dwProductId, __FILE__, __LINE__);
		return TRUE;
	}
	
	if(!pProduct->PushMsg(pMsg, this))
	{
		WriteLog("PushMsg return FALSE");
	}
    return TRUE;
}

BOOL CDBPlayer::ReqReturnSignCost(SignUser &user, CLobbyPlayer *pLobbyPlayer, CProduct *pProduct)
{
	MDExecuteSignCostMsg msg;
	msg.m_nLobbyServerId = pLobbyPlayer->GetServerId();
	msg.m_SignUser.dwUserId = user.dwUserId;
	strncpy(msg.m_SignUser.szUserName, user.szUserName, sizeof(msg.m_SignUser.szUserName) - 1);
	msg.m_SignUser.tmSignup = time(NULL);
	msg.m_SignUser.dwSignupIP = user.dwSignupIP;
	msg.m_SignCost = pProduct->m_stSignCost;
	msg.m_bReturnCost = TRUE;
	SendMsg(&msg);
    return TRUE;
}

BOOL CDBPlayer::AckLoadGameAttr(DMLoadGameAttrMsg *pMsg)
{
	WriteLog("CDBPlayer::AckLoadGameAttr Start %d", pMsg->m_dwUserId);
	CProduct *pProduct = g_pMatchServer->FindProduct(pMsg->m_dwProductId);
	if(pProduct == NULL)
	{
		WriteLog("%s : %d, AckLoadUserAttr pProduct == NULL ProductId=%d", pMsg->m_dwProductId, __FILE__, __LINE__);
		return TRUE;
	}

	CMatch *pMatch = pProduct->FindMatch(pMsg->m_dwMatchId);
	if(pMatch == NULL)
	{
		WriteLog("%s : %d, AckLoadUserAttr pMatch == NULL ProductId=%d", pMsg->m_dwProductId, __FILE__, __LINE__);
		return TRUE;
	}

	if(!pMatch->PushMsg(pMsg, this))
	{
		WriteLog("PushMsg return FALSE");
	}
	return TRUE;
}